﻿using System;
using System.Windows;
using Microsoft.Phone.Controls;
using System.IO;
using System.IO.IsolatedStorage;
using System.Threading;

namespace LuyenThiVer03
{
    public partial class MainPage : PhoneApplicationPage
    {
        
        

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // Hien thi hinh anh screen ban dau
            ShowSplash();
            
            // Test SVN
            //MessageBox.Show("Test SVN");
        }

        private void ShowSplash()
        {
            Thread.Sleep(4000);
        }

        private void LoadDeviceID()
        {
            App a = (App)App.Current;
            byte[] myDeviceID = (byte[])Microsoft.Phone.Info.DeviceExtendedProperties.GetValue("DeviceUniqueId");
            string DeviceIDAsString = Convert.ToBase64String(myDeviceID);
            IsolatedStorageFile iStore = IsolatedStorageFile.GetUserStoreForApplication();

            LoadFile:
            if (iStore.FileExists("idInfo.txt"))
            {
                using (StreamReader sr = new StreamReader(new IsolatedStorageFileStream("idInfo.txt", FileMode.Open, FileAccess.Read, iStore)))
                {
                    
                    try
                    {
                        string deviceID = sr.ReadLine();
                        if (deviceID != DeviceIDAsString)
                        {
                            iStore.DeleteFile("idInfo.txt");
                            goto LoadFile;
                        }
                        else
                        {
                            a.DeviceID = deviceID;
                        }
                    }
                    catch (Exception ex) 
                    {
                        MessageBox.Show(ex.Message);
                    }
                    
                }
            }
            else
            {
                using (StreamWriter sw = new StreamWriter(new IsolatedStorageFileStream("idInfo.txt", FileMode.OpenOrCreate, FileAccess.Write, iStore)))
                {
                    sw.WriteLine(DeviceIDAsString);
                    sw.Close();
                    a.DeviceID = DeviceIDAsString;
                }
            }

            CheckAccountPermission(a.DeviceID);
        }

        // check quyền truy cập của tài khoản theo ID máy
        private void CheckAccountPermission(string deviceID)
        {

        }

        private void btThiThu_Click(object sender, RoutedEventArgs e)
        {            
            NavigationService.Navigate(new Uri("/PageChonMon.xaml?chon=thithu", UriKind.Relative));            
        }

        private void btOnThi_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/PageChonMon.xaml?chon=onthi", UriKind.Relative));    
        }

        private void btTraCuu_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Tính năng sẽ cập nhật trong phiên bản tiếp theo!");
        }

        private void btKetQua_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/PageKetQua.xaml", UriKind.Relative));
        }

        private void btHuongDan_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/PageHuongDan.xaml", UriKind.Relative));
        }

        private void btThongTin_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/PageThongTin.xaml", UriKind.Relative));
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            while (NavigationService.CanGoBack)
            {
                NavigationService.RemoveBackEntry();
            }
        }

        private void btDownload_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/PageDownloadDeThi.xaml", UriKind.Relative));
        }
    }
}